home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / libtiff / tools / ppm2tiff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  6.5 KB  |  242 lines

  1. /* $Header: /usr/people/sam/tiff/tools/RCS/ppm2tiff.c,v 1.26 1996/04/22 22:03:26 sam Rel $ */
  2.  
  3. /*
  4.  * Copyright (c) 1991-1996 Sam Leffler
  5.  * Copyright (c) 1991-1996 Silicon Graphics, Inc.
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and 
  8.  * its documentation for any purpose is hereby granted without fee, provided
  9.  * that (i) the above copyright notices and this permission notice appear in
  10.  * all copies of the software and related documentation, and (ii) the names of
  11.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Sam Leffler and Silicon Graphics.
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  17.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  18.  * 
  19.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24.  * OF THIS SOFTWARE.
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31.  
  32. #include "tiffio.h"
  33.  
  34. #if defined(_WINDOWS) || defined(MSDOS)
  35. #define BINMODE "b"
  36. #else
  37. #define    BINMODE
  38. #endif
  39.  
  40. #define    streq(a,b)    (strcmp(a,b) == 0)
  41. #define    strneq(a,b,n)    (strncmp(a,b,n) == 0)
  42.  
  43. static    uint16 compression = COMPRESSION_LZW;
  44. static    uint16 predictor = 0;
  45. static    int quality = 75;    /* JPEG quality */
  46. static    int jpegcolormode = JPEGCOLORMODE_RGB;
  47.  
  48. static    void usage(void);
  49. static    int processCompressOptions(char*);
  50.  
  51. static void
  52. BadPPM(char* file)
  53. {
  54.     fprintf(stderr, "%s: Not a PPM file.\n", file);
  55.     exit(-2);
  56. }
  57.  
  58. int
  59. main(int argc, char* argv[])
  60. {
  61.     uint16 photometric;
  62.     uint32 rowsperstrip = (uint32) -1;
  63.     double resolution = -1;
  64.     unsigned char *buf = NULL;
  65.     uint32 row;
  66.     tsize_t linebytes;
  67.     uint16 spp;
  68.     TIFF *out;
  69.     FILE *in;
  70.     uint32 w, h;
  71.     int prec;
  72.     char *infile;
  73.     int c;
  74.     extern int optind;
  75.     extern char* optarg;
  76.  
  77.     while ((c = getopt(argc, argv, "c:r:R:")) != -1)
  78.         switch (c) {
  79.         case 'c':        /* compression scheme */
  80.             if (!processCompressOptions(optarg))
  81.                 usage();
  82.             break;
  83.         case 'r':        /* rows/strip */
  84.             rowsperstrip = atoi(optarg);
  85.             break;
  86.         case 'R':        /* resolution */
  87.             resolution = atof(optarg);
  88.             break;
  89.         case '?':
  90.             usage();
  91.             /*NOTREACHED*/
  92.         }
  93.  
  94.     /*
  95.      * If only one file is specified, read input from
  96.      * stdin; otherwise usage is: ppm2tiff input output.
  97.      */
  98.     if (argc - optind > 1) {
  99.         infile = argv[optind++];
  100.         in = fopen(infile, "r" BINMODE);
  101.         if (in == NULL) {
  102.             fprintf(stderr, "%s: Can not open.\n", infile);
  103.             return (-1);
  104.         }
  105.     } else {
  106.         infile = "<stdin>";
  107.         in = stdin;
  108.     }
  109.  
  110.     if (getc(in) != 'P')
  111.         BadPPM(infile);
  112.     switch (getc(in)) {
  113.     case '5':            /* it's a PGM file */
  114.         spp = 1;
  115.         photometric = PHOTOMETRIC_MINISBLACK;
  116.         break;
  117.     case '6':            /* it's a PPM file */
  118.         spp = 3;
  119.         photometric = PHOTOMETRIC_RGB;
  120.         if (compression == COMPRESSION_JPEG &&
  121.             jpegcolormode == JPEGCOLORMODE_RGB)
  122.             photometric = PHOTOMETRIC_YCBCR;
  123.         break;
  124.     default:
  125.         BadPPM(infile);
  126.     }
  127.     if (fscanf(in, " %ld %ld %d", &w, &h, &prec) != 3)
  128.         BadPPM(infile);
  129.     if (getc(in) != '\n' || w <= 0 || h <= 0 || prec != 255)
  130.         BadPPM(infile);
  131.  
  132.     out = TIFFOpen(argv[optind], "w");
  133.     if (out == NULL)
  134.         return (-4);
  135.     TIFFSetField(out, TIFFTAG_IMAGEWIDTH,  w);
  136.     TIFFSetField(out, TIFFTAG_IMAGELENGTH, h);
  137.     TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  138.     TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
  139.     TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
  140.     TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  141.     TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
  142.     TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
  143.     switch (compression) {
  144.     case COMPRESSION_JPEG:
  145.         TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
  146.         TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
  147.         break;
  148.     case COMPRESSION_LZW:
  149.     case COMPRESSION_DEFLATE:
  150.         if (predictor != 0)
  151.             TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
  152.         break;
  153.     }
  154.     linebytes = spp * w;
  155.     if (TIFFScanlineSize(out) > linebytes)
  156.         buf = (unsigned char *)_TIFFmalloc(linebytes);
  157.     else
  158.         buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out));
  159.     TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
  160.         TIFFDefaultStripSize(out, rowsperstrip));
  161.     if (resolution > 0) {
  162.         TIFFSetField(out, TIFFTAG_XRESOLUTION, resolution);
  163.         TIFFSetField(out, TIFFTAG_YRESOLUTION, resolution);
  164.         TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
  165.     }
  166.     for (row = 0; row < h; row++) {
  167.         if (fread(buf, linebytes, 1, in) != 1) {
  168.             fprintf(stderr, "%s: scanline %lu: Read error.\n",
  169.                 infile, (unsigned long) row);
  170.             break;
  171.         }
  172.         if (TIFFWriteScanline(out, buf, row, 0) < 0)
  173.             break;
  174.     }
  175.     (void) TIFFClose(out);
  176.     if (buf)
  177.         _TIFFfree(buf);
  178.     return (0);
  179. }
  180.  
  181. static int
  182. processCompressOptions(char* opt)
  183. {
  184.     if (streq(opt, "none"))
  185.         compression = COMPRESSION_NONE;
  186.     else if (streq(opt, "packbits"))
  187.         compression = COMPRESSION_PACKBITS;
  188.     else if (strneq(opt, "jpeg", 4)) {
  189.         char* cp = strchr(opt, ':');
  190.         if (cp && isdigit(cp[1]))
  191.             quality = atoi(cp+1);
  192.         if (cp && strchr(cp, 'r'))
  193.             jpegcolormode = JPEGCOLORMODE_RAW;
  194.         compression = COMPRESSION_JPEG;
  195.     } else if (strneq(opt, "lzw", 3)) {
  196.         char* cp = strchr(opt, ':');
  197.         if (cp)
  198.             predictor = atoi(cp+1);
  199.         compression = COMPRESSION_LZW;
  200.     } else if (strneq(opt, "zip", 3)) {
  201.         char* cp = strchr(opt, ':');
  202.         if (cp)
  203.             predictor = atoi(cp+1);
  204.         compression = COMPRESSION_DEFLATE;
  205.     } else
  206.         return (0);
  207.     return (1);
  208. }
  209.  
  210. char* stuff[] = {
  211. "usage: ppm2tiff [options] input.ppm output.tif",
  212. "where options are:",
  213. " -r #        make each strip have no more than # rows",
  214. " -R #        set x&y resolution (dpi)",
  215. "",
  216. " -c jpeg[:opts]  compress output with JPEG encoding",
  217. " -c lzw[:opts]    compress output with Lempel-Ziv & Welch encoding",
  218. " -c zip[:opts]    compress output with deflate encoding",
  219. " -c packbits    compress output with packbits encoding",
  220. " -c none    use no compression algorithm on output",
  221. "",
  222. "JPEG options:",
  223. " #        set compression quality level (0-100, default 75)",
  224. " r        output color image as RGB rather than YCbCr",
  225. "LZW and deflate options:",
  226. " #        set predictor value",
  227. "For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
  228. NULL
  229. };
  230.  
  231. static void
  232. usage(void)
  233. {
  234.     char buf[BUFSIZ];
  235.     int i;
  236.  
  237.     setbuf(stderr, buf);
  238.     for (i = 0; stuff[i] != NULL; i++)
  239.         fprintf(stderr, "%s\n", stuff[i]);
  240.     exit(-1);
  241. }
  242.